//
//  StatusCell.swift
//  Weibo10
//
//  Created by Leon on 15/10/19.
//  Copyright © 2015年 Leon. All rights reserved.
//

import UIKit

let statusCellMargin: CGFloat = 6 * scale
let statusHeadIconViewWidth: CGFloat = 35

class StatusCell: UITableViewCell {
    
    /// 视图模型
    var viewModel: StatusViewModel? {
        didSet{
            // statusCell - 头部视图
            headView.viewsModel = viewModel
            // statusCell - 文字内容
            contentLabel.text = viewModel?.status.text
            // statusCell - 尾部视图
            footView.viewModel = viewModel
            // statusCell - 图片视图
            pictureView.viewModel = viewModel
            
            pictureView.snp_updateConstraints { (make) -> Void in
                make.height.equalTo(pictureView.bounds.height)
                make.width.equalTo(pictureView.bounds.width)
            }
            
            
        }
    }
    /// 根据指定的视图模型计算行高
    ///
    /// - parameter vm: 视图模型
    ///
    /// - returns: 返回视图模型对应的行高
    func rowHeight(vm: StatusViewModel) -> CGFloat {
        // 1. 记录视图模型 -> 会调用上面的 didSet 设置内容以及更新`约束`
        viewModel = vm
        
        // 2. 强制更新所有约束 -> 所有控件的frame都会被计算正确
        contentView.layoutIfNeeded()
        
        // 3. 返回底部视图的最大高度
        return CGRectGetMaxY(footView.frame)
    }
    
    ///  构造函数
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    /// statusCell - 头部视图
    private lazy var headView: StatusCellHeadView = StatusCellHeadView()
    /// statusCell - 文字内容
    lazy var contentLabel: UILabel = UILabel(title: "微博正文",
        fontSize: 15,
        color: UIColor.darkGrayColor(),
        screenInset: statusCellMargin)
    /// statusCell - 尾部视图
    lazy var footView: StatusCellFootView = StatusCellFootView()
    /// statusCell - 尾部视图
    lazy var pictureView: StatusCellPictureView = StatusCellPictureView()
}

// MARK: - 设置UI
extension StatusCell {
    
    func setupUI() {
        // 1 添加控件
        contentView.addSubview(headView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(footView)
        
        // 2 自动布局
        headView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.width.equalTo(contentView.snp_width)
            make.height.equalTo(statusHeadIconViewWidth + statusCellMargin + sepLineViewHeight)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headView.snp_bottom).offset(statusCellMargin)
            make.left.equalTo(contentView.snp_left).offset(statusCellMargin)
            
        }
        
        
        footView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(pictureView.snp_bottom).offset(statusCellMargin)
            make.width.equalTo(contentView.snp_width)
            make.height.equalTo(44)
           // make.bottom.equalTo(contentView.snp_bottom)
        }
    }
}




